//
//  UIBarButtonItem-Extension.swift
//  douyuzhibo
//
//  Created by 杜方宜 on 2017/7/13.
//  Copyright © 2017年 pelesys-luoyang. All rights reserved.
//

import UIKit
extension UIBarButtonItem {
    /*
     class func createItem(imageName : String, highImageName : String, size : CGSize) -> UIBarButtonItem {
     let btn = UIButton()
     
     btn.setImage(UIImage(named: imageName), forState: .Normal)
     btn.setImage(UIImage(named: highImageName), forState: .Highlighted)
     
     btn.frame = CGRect(origin: CGPointZero, size: size)
     
     return UIBarButtonItem(customView: btn)
     }
     */
    
    // 便利构造函数: 1> convenience开头 2> 在构造函数中必须明确调用一个设计的构造函数(self)
    convenience public  init(imageName : String, highImageName : String = "", size : CGSize = CGSize.zero) {
        // 1.创建UIButton
        let btn = UIButton()
        btn.setImage(UIImage(named: imageName), for: UIControlState())
         // 2.设置btn的图片
        if highImageName != "" {
            btn.setImage(UIImage(named: highImageName), for: .highlighted)
        }
         // 3.设置btn的尺寸
        if size == CGSize.zero {
            btn.sizeToFit()
        }else{
            btn.frame = CGRect(origin: CGPoint.zero, size: size);
        }
         // 4.创建UIBarButtonItem
        self.init(customView:btn)
    }
}
